<script setup lang="ts">
import { onMounted, ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { usePublicHooks, FormProps } from "@/views/hooks";
import { getDictList, Dict } from "@/api/system";

const props = withDefaults(defineProps<FormProps<Dict>>(), {
  formInline: () => ({
    id: "",
    parentId: undefined,
    label: "",
    code: "",
    color: "",
    description: "",
    state: true,
    sort: 1
  })
});
const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const options = ref<Array<any>>([]);
const disabled = !!newFormInline.value.id;

function getRef() {
  return ruleFormRef.value;
}

function loadDictTop() {
  getDictList({ parentId: "0", state: true }).then(data => {
    const top = { id: "0", label: "顶级字典" };
    options.value = [top, ...data];
  });
}

onMounted(() => {
  loadDictTop();
});

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col>
        <el-form-item label="上级字典" prop="parentId">
          <el-select-v2
            v-model="newFormInline.parentId"
            :options="options"
            :props="{
              value: 'id'
            }"
            clearable
            placeholder="请选择上级字典"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="字典名称" prop="label">
          <el-input
            v-model="newFormInline.label"
            clearable
            placeholder="请输入字典名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item v-if="!disabled" label="字典标识" prop="code">
          <el-input
            v-model="newFormInline.code"
            clearable
            placeholder="请输入字典编码"
          />
        </el-form-item>
        <el-form-item v-if="disabled" label="字典标识">
          <el-input
            v-model="newFormInline.code"
            clearable
            placeholder="请输入字典编码"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="字典编码" prop="value">
          <el-input
            v-model="newFormInline.value"
            clearable
            placeholder="请输入字典编码"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="标签颜色" prop="color">
          <el-input
            v-model="newFormInline.color"
            clearable
            placeholder="请输入标签颜色"
          />
        </el-form-item>
      </re-col>
      <re-col>
        <el-form-item label="字典描述" prop="description">
          <el-input
            v-model="newFormInline.description"
            clearable
            placeholder="请输入字典描述"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="字典排序" prop="sort">
          <el-input-number
            v-model="newFormInline.sort"
            :min="1"
            :max="9999"
            controls-position="right"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="字典状态">
          <el-switch
            v-model="newFormInline.state"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
